﻿using System;
using System.Linq;
using Avalonia.Input;
using Avalonia.Interactivity;
using AvaloniaEdit.CodeCompletion;
using MarkdownRow.Edit.CompletionData;

namespace MarkdownRow.EditView;

public partial class EvenRenderingEditor : Editor
{
    
    public EvenRenderingEditor(string context)
    {
        InitializeComponent();
        Init(context);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        var a = GetMain();
        if(a == null)return;
        Editor.Options.ShowTabs = a.Setting.EditorSetting.IsShowTabs;
        Editor.Options.ShowSpaces = a.Setting.EditorSetting.IsShowSpaces;
        Editor.Options.ShowEndOfLine = a.Setting.EditorSetting.IsShowEOL;
        doc = a.Doc;
    }

    public override void ContextChange(string s)
    {
        Editor.Text = s;
    }

    public override void CopyMenuClick(object? sender, RoutedEventArgs e)
    {
        
    }

    private void TextChanged(object? sender, EventArgs e)
    {
        Markdown.Markdown = Editor.Text;
    }
    
    private void EditorKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.LeftAlt)
        {
            var list = doc.Using.Select(x => x.ToUsing());
            CompletionWindow = new CompletionWindow(Editor.TextArea);
            CompletionWindow.Closed += (_, _) => CompletionWindow = null;
            var data = CompletionWindow.CompletionList.CompletionData;
            foreach (var item in list)
                data.Add(item);

            CompletionWindow.Show();
        }
    }

    #region ContextMenu

    public override void ShowTabs(object? sender, RoutedEventArgs e)
    {
        Editor.Options.ShowTabs = !Editor.Options.ShowTabs;
    }

    public override void ShowSpaces(object? sender, RoutedEventArgs e)
    {
        Editor.Options.ShowSpaces = !Editor.Options.ShowSpaces;
    }

    public override void ShowEOL(object? sender, RoutedEventArgs e)
    {
        Editor.Options.ShowEndOfLine = !Editor.Options.ShowEndOfLine;
    }

    public override void SearchMenuClick(object? sender, RoutedEventArgs e)
    {
        
    }

    public override void PasteMenuClick(object? sender, RoutedEventArgs e)
    {
        
    }

    public override void CutMenuClick(object? sender, RoutedEventArgs e)
    {
        
    }

    public override string GetText()
    {
        return Editor.Text;
    }

    #endregion


    
}